
package group3equationsolver;

/**
 * 
 * the solve class of the program, use to calculate the input equation
 * It return the String answer of the equation
 * 
 * @author Group 3
 * @member Nguyễn Hùng Phi
 * @member Hoàng Văn Hậu
 * @member Thái Trọng Hiếu
 * @member Dương Dinh Phúc
 */
public class Solver {
	/**
	 * This method use to solve x in the equation: a*x + b = 0
	 *
	 * @param numberA variable for a in the equation
	 * @param numberB variable for b in the equation
	 * @return 	return the string telling how many solutions the equation have
	 * 			and the value of solution if possible
	 */
	public String solve1(double numberA, double numberB){
		//check if a != 0
		if(numberA==0){
			//if a = o, the equation become b = 0
			//check b
			if(numberB==0){
				//if b == 0. then the solution have infinite number of solutions
				return "The equation has infinite number of solutions";
			}else{
				//if b != 0 then the equation has no solution
				return "The equation has no solution";
			}
		}
		//if a != 0 then we can calculate x = b/a
		return "The equation has one solusion: \nx = " + (numberB/numberA);
	}
	/** ngay
	 * 
	 * This method use to solve the equation: a*x^2 + b*x + c = 0
	 * 
	 * @param numberA variable for a in the equation
	 * @param numberB variable for b in the equation
	 * @param numberC variable for c in the equation
	 * @return 	the string telling how many solutions the equation have
	 * 			and the value of solution if possible
	 */
	public String solve2( double numberA, double numberB, double numberC){
		//first check if a!= 0
		if(numberA==0) {
			//if a == 0, the equation become b*x + c = 0
			//call the solve1 method for the equation
			return solve1(numberB,numberC);
		}else{
			//if a!= 0, we calculate the delta value of the equation
			// delta = b^2 - 4*a*c
			double delta = numberB*numberB-4*numberA*numberC;
			//check delta
			if(delta < 0) {
				//if delta < 0 then the equation has no solution
				return "The equation has no solution";
			}
			//check delta
			if(delta == 0) {
				//if delta == 0 then the equation has one solution x = -b/(2*a)
				return "The equation has one solution: x = " + (-numberB/2*numberA); 
			}
			//this is when delta > 0
			// change delta value into: delta = sqrt(delta)
			delta = Math.sqrt(delta);
			//declare the variable for first solution x1 = (-b-delta)/(2*a)
			double x1; 
			x1 = (-numberB-delta)/(2*numberA);
			//declare the variable for first solution x1 = (-b+delta)/(2*a)
			double x2;
			x2 = (-numberB+delta)/(2*numberA);
			//return the string
			return "The equation has two solusions: \nx1 = " + x1 +"\nx2 = " + x2;
		}
	}
}
